package com.ge.healthcare.autosc.common.message;

/*
 * $Id: AutoSCBasicMDP.java,v 1.4 2009/12/16 18:34:55 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
import java.util.Date;

import org.springframework.beans.factory.FactoryBean;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;
import com.ge.healthcare.autosc.common.dbLogger.StepLogger;
import com.ge.healthcare.autosc.common.message.converter.AutoSCMessage;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.common.util.TimeFormatStandard;

public class AutoSCBasicMDP implements FactoryBean {
	static final String ERRMSG_TASK_ID_IS_NULL = ApplicationInfo.getInstance()
			.getProperty(PropertyConstants.ERRMSG_TASK_ID_IS_NULL);
	protected static ITaskDAO taskDAO = null;
	protected ThreadLocal<String> codeId = new ThreadLocal<String>();
	protected static ICodeDetailsDAO codeDAO = null;

	protected void setCurrentCodeId(String statusCodeName) {
		this.codeId.set(statusCodeName);
	}

	protected void startTaskLogging(ActTask task, String statusCode) {
		setCurrentCodeId(statusCode);
		startStepLog(task, statusCode);
		setTaskCode(task);
	}

	protected String getCurrentCodeId(String codeDefault) {
		if (ASCUtil.isNullOrEmpty(codeId.get())) {
			codeId.set(codeDefault);
		}
		return this.codeId.get();
	}

	protected void setTaskCode(ActTask task) {
		if (isValidTask(task)) {
			taskDAO.setCodeDetailsAlways(task, this.codeId.get());
		}
	}

	/*
	 * Report the task progress
	 */
	protected void updateStepType(ActTask actTask) {
		actTask.setStepTypeId(StepLogger.getStepTypeId());
		taskDAO.saveAlways(actTask);
	}

	protected void setTaskCode(ActTask task, String code) {
		if (isValidTask(task)) {
			taskDAO.setCodeDetailsAlways(task, code);
		}
	}

	protected void startStepLog(ActTask task, String statusCode) {
		String methodName = "startStepLog";
		ASCLogger.debug(this.getClass(), methodName, baseMsg(task,
				"Logging Start "));
		if (isValidTask(task)) {
			setCurrentCodeId(statusCode);
			StepLogger.logStartTime(task.getTaskId(), StepLogger.TASK_TYPE);
		}
	}

	/**
	 * Logs end event and updates StepLogger
	 * 
	 * @param codeId
	 *            - Code id to use for end event
	 * @param taskInfo
	 *            - Task Info object
	 */
	protected void logEndProcessing(ActTask task, String defaultCode) {
		if (ASCUtil.isNullOrEmpty(codeId.get())) {
			setCurrentCodeId(defaultCode);
		}
		String methodName = "logEndProcessing";
		ASCLogger.debug(this.getClass(), methodName,
				baseMsg(task, "Completed "));
		Long codeId = taskDAO.setCodeDetailsAlways(task, this.codeId.get());
		StepLogger.logEndTime(codeId);
	}

	/**
	 * Logs end event and updates StepLogger
	 * 
	 * @param codeId
	 *            - Code id to use for end event
	 * @param taskInfo
	 *            - Task Info object
	 */
	protected void stepLoggerEnd(AutoSCMessage task, String defaultCode) {
		if (ASCUtil.isNullOrEmpty(codeId.get())) {
			setCurrentCodeId(defaultCode);
		}
		String methodName = "stepLoggerEnd";
		ASCLogger.debug(this.getClass(), methodName,
				baseMsg(task, "Completed "));
		CfgCodeDetails code = codeDAO
				.getUniqueCodedetailsByCodeName(this.codeId.get());
		StepLogger.logEndTime(code.getCodeId());
	}

	protected StringBuffer baseMsg(AutoSCMessage task, String prepend) {
		StringBuffer msg = new StringBuffer(prepend);
		msg.append("Task: ");
		msg.append(task.getObjectId());
		msg.append(" at ").append(
				TimeFormatStandard.getDtInEndUserDisplayFormat(new Date()));
		return msg;
	}

	/**
	 * Check that task has a TaskId
	 * 
	 * @param task
	 * @return
	 */
	protected boolean isValidTask(ActTask task) {
		String methodName = "isValidTask";
		boolean isValid = true;
		if (!task.isValidId()) {
			Exception exception = new Exception(ERRMSG_TASK_ID_IS_NULL);
			ASCLogger.error(this.getClass(), methodName,
					ERRMSG_TASK_ID_IS_NULL, exception);
			isValid = false;
		}
		return isValid;
	}

	/**
	 * @param actTaskDAO
	 *            the actTaskDAO to set
	 */
	public void setActTaskDAO(ITaskDAO actTaskDAO) {
		AutoSCBasicMDP.taskDAO = actTaskDAO;
	}

	public void setCodeDAO(ICodeDetailsDAO codeDAO) {
		AutoSCBasicMDP.codeDAO = codeDAO;
	}

	public Object getObject() throws Exception {
		return this.getClass().newInstance();
	}

	public Class<?> getObjectType() {
		return this.getClass();
	}

	public boolean isSingleton() {
		return false;
	}

}
